'use strict';

const Controller = require('egg').Controller;                //从 egg 框架引入 Controller

class MainController extends Controller {
    async index() {
        this.ctx.body = 'hi'

    }


    //判断用户名密码是否正确  
    async checkLogin() {
        // this.ctx.body = 'hi'
        let userName = this.ctx.request.body.userName;  //通过 ctx 的request方法拿到 body 中的useName 这个useName是在后台规定的 
        let passWord = this.ctx.request.body.passWord;
        // 从后台的admin_user表查询 
        const sql = "SELECT userName FROM admin_user WHERE userName = '" + userName + "' AND  passWord = '" + passWord + "'"

        const res = await this.app.mysql.query(sql)
       
        if (res.length > 0) {     //如果得到的 res 数组长度大于0 则表示找到了登录记录 ,即登录成功     
            //登录成功,进行session缓存
            let openId = new Date().getTime()   // 用时间戳来做登录密钥，openId要传到前台
            this.ctx.session.openId = { 'openId': openId }
            this.ctx.body = { 'data': '登录成功', 'openId': openId }
        } else {
            this.ctx.body = { 'data': '登录失败' }
        }
    }

    async getTypeInfo(){
        const resType = await this.app.mysql.select('type')
        this.ctx.body={data:resType}
    }

    async addArticle (){
        let tmpArticle = this.ctx.request.body
        const result  = await this.app.mysql.insert('article',tmpArticle)
        const insertSuccess = result.affectedRows === 1
        const insertId = result.insertId

        this.ctx.body = {
            isSuccess : insertSuccess,
            insertId : insertId
        }
    }

    async updateArticle (){
        let tmpArticle = this.ctx.request.body
        const result  = await this.app.mysql.update('article',tmpArticle)
        const uptateSuccess = result.affectedRows === 1
        
        this.ctx.body = {
            isSuccess : uptateSuccess
        }
    }

    async getArticleList(){

        let sql = 'SELECT article.id as id ,' +
                  'article.title as title ,' +
                  'article.introduce as introduce ,' +
                  "FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime,"+
                  'article.view_count as view_count ,' +
                  'type.typeName as typeName ' +
                  'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
                  'ORDER BY article.id DESC'

        const  resList  =  await this.app.mysql.query(sql) 
        this.ctx.body={list : resList}          
    }

    async delArticle(){
        let id = this.ctx.params.id
        const res = await this.app.mysql.delete('article',{'id': id })
        this.ctx.body = {data : res}

    }
    // async getArticleById(){
    //     let id  = this.ctx.params.id
    //     let sql = 'SELECT article.id as id,'+
    //     'article.title as title,'+
    //     'article.introduce as introduce ,'+
    //     'article.article_content as article_content,'+
    //     "FROM_UNIXTIME(article.addTime,'%Y-%m-%d') as addTime,"+
    //     'article.view_count as view_count,' +
    //     'type.typeName as typeName,' +
    //     'type.id as typeId' +
    //     'FROM article LEFT JOIN type ON article.type_id = type.Id '+
    //     'WHERE article.id ='+ id
    //     const result = await this.app.mysql.query(sql)
    //     this.ctx.body = {data : result}

    //  }
    async getArticleById(){
        let id = this.ctx.params.id
    
        let sql = 'SELECT article.id as id,'+
        'article.title as title,'+
        'article.introduce as introduce,'+
        'article.article_content as article_content,'+
        "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime,"+
        'article.view_count as view_count ,'+
        'type.typeName as typeName,'+
        'type.id as typeId '+
        'FROM article LEFT JOIN type ON article.type_id = type.Id '+
        'WHERE article.id='+id
        const result = await this.app.mysql.query(sql)
        this.ctx.body={data:result}
    }
}

module.exports = MainController